﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class AnalyseMethodAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {
                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取分析方法信息
        /// </summary>
        public void GetMethodList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<BD_AnalyseMethod, bool>> exp = SearchExpression.FindByGroup<BD_AnalyseMethod>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.BD_AnalyseMethodDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
            count = DALSession.Current.BD_AnalyseMethodDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }


        /// <summary>
        /// 根据q获取国标信息
        /// </summary>
        public void GetMethodByQ()
        {
            string q = Request.Form["q"];
            if (q == null)
            {
                var list = DALSession.Current.BD_AnalyseMethodDAL.GetListBy(li => li.ID != null);
                Response.Write(JsonHelper.toJson(list));
            }
            else
            {
                var list = DALSession.Current.BD_AnalyseMethodDAL.GetListBy(li => li.AnalyseMethod.Contains(q));
                Response.Write(JsonHelper.toJson(list));
            }
        }

        /// <summary>
        /// 修改分析方法信息
        /// </summary>
        public void EditMethod()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            BD_AnalyseMethod perNew = DALSession.Current.BD_AnalyseMethodDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);
            DALSession.Current.BD_AnalyseMethodDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_AnalyseMethodDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 添加分析方法
        /// </summary>
        public void AddMethod()
        {
            BD_AnalyseMethod per = new BD_AnalyseMethod();
            ModelTools.Form2EditModel(Request.Form, per);
            DALSession.Current.BD_AnalyseMethodDAL.Add(per);
            DALSession.Current.BD_AnalyseMethodDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 删除分析方法
        /// </summary>
        public void DelMethod()
        {
            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            DALSession.Current.BD_AnalyseMethodDAL.DelBy(p => p.ID == Id);
            DALSession.Current.BD_AnalyseMethodDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }


    }
}